package cn.hackcloud.mutithread.juc;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.iteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;

public class TestListJob {
	static ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
	static CopyOnWriteArrayList<Integer> interfaceId = new CopyOnWriteArrayList<Integer>();
	static {
		for (int i = 1; i <= 11; i++) {
			interfaceId.add(i);
		}
	}

	public static void main(String[] args) {
		// 每条数据开启一条线程
		int threadSize = 1;
		// 总数据条数
		int dataSize = interfaceId.size();
		// 线程数
		int threadNum = dataSize / threadSize + 1;
		// 定义标记,过滤threadNum为整数
		boolean special = dataSize % threadSize == 0;
		List<Future<Integer>> futures = new ArrayList<Future<Integer>>();
		List<Integer> subList = null;
		for (int i = 0; i < threadNum; i++) {
			if (i == threadNum - 1) {
				if (special) {
					break;
				}
				subList = interfaceId.subList(threadSize * i, dataSize);
			} else {
				subList = interfaceId.subList(threadSize * i, threadSize * (i + 1));
			}
			final List<Integer> listStr = subList;
			futures.add(executor.submit(new Callable<Integer>() {
				@Override
				public Integer call() throws Exception {
					System.out.println(Thread.currentThread().getName() + ":" + listStr);
					return 1;
				}

			}));
		}
	}

}
